package com.spring.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class ParamController {


    /*
    * @RequestParam
    *
    *   name 设置参数名字
    *    defaultValue 默认参数
    *    required = true 是否必填  默认是false
    * */
    @RequestMapping("/param")
    public String param(@RequestParam(name = "ids",required = true) int id){
        System.out.println(id);
        return  "success";
    }

    /*
    * 获取请求头 @RequestHeader("请求报头")
    * */
    @RequestMapping("/getHead")
    public String getHead(@RequestHeader("Accept-Language") String cookie ){
        System.out.println(cookie);
        return  "success";
    }
// Hm_lvt_fe3b7a223fc08c795f0f4b6350703e6f = 1665406729,1665409568,1665723973;
// JSESSIONID = 0BDB88FFC06F099664768C76A0186FE5
// Accept-Language:zh-CN,zh;q=0.9,en;q=0.8

    /*
    * @CookieValue("JSESSIONID")  获取cookie值
    * */

    @RequestMapping("/getCookval")
    public String getCookval(@CookieValue("JSESSIONID") String cookie ){
        System.out.println(cookie); // B7847F85327A24ABBCE98EDDEE99B583
        return  "success";            //B7847F85327A24ABBCE98EDDEE99B583
    }

    @RequestMapping("/servletAPI")
    @ResponseBody
    public String servletAPI(HttpServletRequest req, HttpServletResponse resp, HttpSession session) throws IOException {
      //  System.out.println(req.getParameter("user"));
        //resp.getWriter().write("hello");
        System.out.println(session);
        return  "success";            //B7847F85327A24ABBCE98EDDEE99B583
    }

    /*
    *   3种  1.返回字符串逻辑视图
    *        2.ServletAPI 跳转
    * */

    @RequestMapping("/returnString")
    public String returnString(){
        return  "success";
    }

    @RequestMapping("/returnvoid")
    public void returnvoid(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException {
//        req.getParameter("user");
//        resp.getWriter().write("hello");
        /* request  域中*/
          req.setAttribute("username","张三");
        /*可以渲染安全目录下的页面*/
//        req.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(req,resp);
         resp.sendRedirect("/index.jsp");
    }



    /* 3种*/
    @RequestMapping("/model")
    public ModelAndView model(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","张三");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/success")
    public ModelAndView Remodel(ModelAndView modelAndView){
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("username","张三");
//        modelAndView.setViewName("success");
        modelAndView.addObject("username","张三");
        return modelAndView;
    }


    @RequestMapping("/redirect")
    public String redict(){
        return "redirect:/index.jsp";
    }


    @RequestMapping("/dispatcher")
    public String dispatcher(){
        return "forward:/WEB-INF/pages/success.jsp";
    }

}
